***************************************************
*** DONUT ROBUSTNESS CHECKS					    ***
*** 1) CHECK THAT THE RESULTS ARE ROBUST TO     ***
***    REMOVING HOSPITALS CLOSEST TO THE BORDER ***
*** 2) CHECK THAT RESULTS ARE ROBUST TO USING   ***
*** 	NON-NEIGHBORING HOSP IN JACKKNIFE 	    ***
*** Last edited: 6/10/21         		   	    ***
***************************************************
log using "${CodePath}/log/regs_hosp_100mradius_donut_$S_DATE.log", text replace

cap mkdir "${OutputPath}/RACRegion/jackknife_hrr_audit_in2011/"
cap mkdir "${OutputPath}/RACRegion/jackknife_hrr_audit_in2011/hospcomp_100m/"
cap mkdir "${OutputPath}/RACRegion/jackknife_hrr_audit_in2011/hospcomp_100m/Event Studies/"
cap mkdir "${OutputPath}/RACRegion/jackknife_hrr_audit_in2011/hospcomp_100m/Event Studies/nearseg100_state_2__state"
cap mkdir "${OutputPath}/RACRegion/jackknife_hrr_audit_in2011/hospcomp_100m/Event Studies/nearseg100_state_2__state/CI95/"

cap mkdir "${OutputPath}/RACRegion/jackknife_RACregion_hrr_audit_in2011/"
cap mkdir "${OutputPath}/RACRegion/jackknife_RACregion_hrr_audit_in2011/hospcomp_100m/"
cap mkdir "${OutputPath}/RACRegion/jackknife_RACregion_hrr_audit_in2011/hospcomp_100m/Event Studies/"
cap mkdir "${OutputPath}/RACRegion/jackknife_RACregion_hrr_audit_in2011/hospcomp_100m/Event Studies/nearseg100_state_2__state/"
cap mkdir "${OutputPath}/RACRegion/jackknife_RACregion_hrr_audit_in2011/hospcomp_100m/Event Studies/nearseg100_state_2__state/CI95/"

cap mkdir "${OutputPath}/RACRegion/jackknife_chain_audit_in2011/"
cap mkdir "${OutputPath}/RACRegion/jackknife_chain_audit_in2011/hospcomp_100m/"
cap mkdir "${OutputPath}/RACRegion/jackknife_chain_audit_in2011/hospcomp_100m/Event Studies/"
cap mkdir "${OutputPath}/RACRegion/jackknife_chain_audit_in2011/hospcomp_100m/Event Studies/nearseg100_state_2__state"
cap mkdir "${OutputPath}/RACRegion/jackknife_chain_audit_in2011/hospcomp_100m/Event Studies/nearseg100_state_2__state/CI95/"


*--------------------------------------------------
* Select and create dataset for regressions
*--------------------------------------------------


use "${DataPath}/within100m/pnlist_groups_100m.dta", clear
duplicates drop hospcomp_group_100 pn, force
expand 9
sort pn
bys pn hospcomp_group_100 : gen fyear = _n + 2006
preserve
	use "${DataPath}/derived/hospyear_0716_jk.dta", clear

	keep pn fyear dist_to_border jk_RACregion_audit_in2011* jk_state_audit_in2011* jkhrr_state_* jkhrr_RAC_* audit_in2011* jk_RACregion_audit_in2011* demandrate_in2011* jk_RACregion_demand_in2011*  jk_state_demand_in2011* log_n_claims nearseg100_state_2 nearseg100_state RACregion state jkchn*
	keep if fyear >= 2007 & fyear <= 2015
	tempfile temp
	save `temp'
restore

merge m:1 pn fyear using `temp', keep(1 3) gen(_jkmerge)


egen hospcomp_group_fyear = group(hospcomp_group_100 fyear)
egen state_splitid = group(state split_id)
egen state_nearseg100 = group(state nearseg100_state)

*--------------------------------------------------
* Regressions with hospitals closest to the border removed
*--------------------------------------------------

ivreghdfe  log_n_claims (audit_in2011_X2007 audit_in2011_X2008 audit_in2011_X2009 audit_in2011_X2011 audit_in2011_X2012 audit_in2011_X2013 audit_in2011_X2014 audit_in2011_X2015 ///
		 			= jk_state_audit_in2011_X2007 jk_state_audit_in2011_X2008 jk_state_audit_in2011_X2009 jk_state_audit_in2011_X2011 jk_state_audit_in2011_X2012 jk_state_audit_in2011_X2013 jk_state_audit_in2011_X2014 jk_state_audit_in2011_X2015) ///
					if dist_to_border >= 10 ///
					,absorb(hospcomp_group_fyear pn) cluster(state nearseg100_state_2) 
iv_event_study95_yscale  audit_in2011 -0.06 0.06 0.03
graph export "${OutputPath}/RACRegion/jackknife_audit_in2011/hospcomp_100m/Event Studies/nearseg100_state_2__state/CI95/outside10m_log_n_claims_IV.png", replace


*--------------------------------------------------
* Jackknife with hospitals in same state but different HRR -- "donut" jackknife
*--------------------------------------------------
ivreghdfe  log_n_claims (audit_in2011_X2007 audit_in2011_X2008 audit_in2011_X2009 audit_in2011_X2011 audit_in2011_X2012 audit_in2011_X2013 audit_in2011_X2014 audit_in2011_X2015 ///
			= jkhrr_state_audit_in2011_X2007 jkhrr_state_audit_in2011_X2008 jkhrr_state_audit_in2011_X2009 jkhrr_state_audit_in2011_X2011 jkhrr_state_audit_in2011_X2012 jkhrr_state_audit_in2011_X2013 jkhrr_state_audit_in2011_X2014 jkhrr_state_audit_in2011_X2015) ///
		,absorb(hospcomp_group_fyear pn) cluster(state nearseg100_state_2) 
iv_event_study95_yscale audit_in2011  -0.06 0.06 0.03
graph export "${OutputPath}/RACRegion/jackknife_hrr_audit_in2011/hospcomp_100m/Event Studies/nearseg100_state_2__state/CI95/log_n_claims_IV.png", replace 


*--------------------------------------------------
* Jackknife with hospitals in same RAC region but different HRR -- "donut" jackknife
*--------------------------------------------------
// Event studies
local hospvars    "`hospvars' log_n_claims"
foreach hospvar of varlist `hospvars' {

		
			reghdfe  `hospvar' jkhrr_RAC_audit_in2011_X2*, absorb(pn hospcomp_group_fyear) cluster(nearseg100_state_2 state)
			event_study95 jkhrr_RAC_audit_in2011
			graph export "${OutputPath}/RACRegion/jackknife_RACregion_hrr_audit_in2011/hospcomp_100m/Event Studies/nearseg100_state_2__state/CI95/`hospvar'.png", replace

			ivreghdfe  `hospvar' (audit_in2011_X2007 audit_in2011_X2008 audit_in2011_X2009 audit_in2011_X2011 audit_in2011_X2012 audit_in2011_X2013 audit_in2011_X2014 audit_in2011_X2015 ///
		 			= jkhrr_RAC_audit_in2011_X2007 jkhrr_RAC_audit_in2011_X2008 jkhrr_RAC_audit_in2011_X2009 jkhrr_RAC_audit_in2011_X2011 jkhrr_RAC_audit_in2011_X2012 jkhrr_RAC_audit_in2011_X2013 jkhrr_RAC_audit_in2011_X2014 jkhrr_RAC_audit_in2011_X2015) ///
					,absorb(hospcomp_group_fyear pn) cluster(state nearseg100_state_2) 
			iv_event_study95_yscale audit_in2011  -0.06 0.06 0.03
			graph export "${OutputPath}/RACRegion/jackknife_RACregion_hrr_audit_in2011/hospcomp_100m/Event Studies/nearseg100_state_2__state/CI95/`hospvar'_IV.png", replace 
	
} // hospvar

*--------------------------------------------------
* Jackknife with hospitals in same state but not in the same chain in 2010 -- "donut" jackknife
*--------------------------------------------------

ivreghdfe  log_n_claims (audit_in2011_X2007 audit_in2011_X2008 audit_in2011_X2009 audit_in2011_X2011 audit_in2011_X2012 audit_in2011_X2013 audit_in2011_X2014 audit_in2011_X2015 ///
			= jkchn_state_audit_in2011_X2007 jkchn_state_audit_in2011_X2008 jkchn_state_audit_in2011_X2009 jkchn_state_audit_in2011_X2011 jkchn_state_audit_in2011_X2012 jkchn_state_audit_in2011_X2013 jkchn_state_audit_in2011_X2014 jkchn_state_audit_in2011_X2015) ///
		,absorb(hospcomp_group_fyear pn) cluster(state nearseg100_state_2) 
iv_event_study95_yscale audit_in2011 -0.06 0.06 0.03
graph export "${OutputPath}/RACRegion/jackknife_chain_audit_in2011/hospcomp_100m/Event Studies/nearseg100_state_2__state/CI95/log_n_claims_IV.png", replace 


log close


